<?php

defined('IN_IA') or Workerman\Protocols\Http::end('Access Denied');
@Workerman\Protocols\Http::sessionStart();

require_once IA_ROOT ."/../Libs/function/comm.func.php";



/**
 * 执行一条非查询语句
 *
 * @param string $sql
 * @param array or string $params
 * @return mixed
 *          成功返回受影响的行数
 *          失败返回FALSE
 */
function pdo_queryExec($sql)
{
	//if(strpos(strtoupper($sql), "UPDATE") || strpos(strtoupper($sql), "INSERT") || strpos(strtoupper($sql), "DELETE")){
	//}
    return sql_queryExec($sql);
}


/**
 * 执行一条非查询语句
 *
 * @param string $sql
 * @param array or string $params
 * @return mixed
 *          成功返回受影响的行数
 *          失败返回FALSE
 */
function pdo_query($sql)
{
	//if(strpos(strtoupper($sql), "UPDATE") || strpos(strtoupper($sql), "INSERT") || strpos(strtoupper($sql), "DELETE")){
	//}
    return sql_query($sql);
}

/**
 * 执行SQL返回第一行
 *
 * @param string $sql
 * @param array $params
 * @return mixed
 */
function pdo_fetch($sql)
{
	if(strpos(strtolower($sql), "limit")==false)
		$sql = rtrim($sql, ";") . " limit 1;";
	
	$ret = sql_query($sql);
	if(count($ret)>0){
		return $ret[0];
	}else{
		return null;
	}
}

/**
 * 执行SQL返回全部记录
 *
 * @param string $sql
 * @param array $params
 * @return mixed
 */
function pdo_fetchall($sql)
{
	return sql_query($sql);
	//var_dump($ret);
    //return pdo()->fetchall($sql, $params, $keyfield);
}


function pdo_fetchallidx($sql)
{
	return sql_queryIdx($sql);
    //return pdo()->fetchallidx($sql, $params, $keyfield);
}

/**
 * 更新记录
 *
 * @param string $table
 * @param array $data
 *          要更新的数据数组
 *          array(
 *              '字段名' => '值'
 *          )
 * @param array $params
 *          更新条件
 *          array(
 *              '字段名' => '值'
 *          )
 * @param string $glue
 *          可以为AND OR
 * @return mixed
 */
function pdo_update($table, $data = array(), $params = array())
{
	//拆分data和params到sql语句中
	$sql = "UPDATE $table SET ";
	foreach($data as $k=>$v){
		$sql .= "`$k`='$v',";
	}
	$sql = rtrim($sql, ",");
	$sql .= " WHERE ";
	foreach($params as $k=>$v){
		$sql .= "`$k`='$v' AND ";
	}
	$sql = substr($sql, 0, -4) . ";";
	return sql_query($sql);
    //return pdo()->update($table, $data, $params, $glue);
}

/**
 * 更新记录
 *
 * @param string $table
 * @param array $data
 *          要更新的数据数组
 *          array(
 *              '字段名' => '值'
 *          )
 * @param boolean $replace
 *          是否执行REPLACE INTO
 *          默认为FALSE
 * @return mixed
 */
function pdo_insert($table, $data = array())
{
	//拆分data和params到sql语句中
	$keys = "(";
	$vals = "(";
	foreach($data as $k=>$v){
		$keys .= "`$k`,";
		$vals .= "'$v',";
	}
	$keys = rtrim($keys, ","). ")";
	$vals = rtrim($vals, ","). ")";
	
	$sql = "INSERT INTO $table $keys VALUES $vals;";
	//var_dump($sql);
	return sql_query($sql);
    //return pdo()->insert($table, $data, $replace);
}

/**
 * 删除记录
 *
 * @param string $table
 * @param array $params
 *          更新条件
 *          array(
 *              '字段名' => '值'
 *          )
 * @param string $glue
 *          可以为AND OR
 * @return mixed
 */
function pdo_delete($table, $params = array())
{
	$sql = "DELETE FROM $table WHERE ";
	foreach($params as $k=>$v){
		$sql .= "`$k`='$v',";
	}
	$sql = rtrim($sql, ",").";";
	return sql_query($sql);
    //return pdo()->delete($table, $params, $glue);
}

/**
 * 返回lastInsertId
 *
 */
function pdo_insertid()
{
    return sql_lastInsertId();
}

function pdo_begin()
{
    sql_beginTrans();
}

function pdo_commit()
{
    sql_commitTrans();
}

function pdo_rollback()
{
    sql_rollBackTrans();
}
